import { Dimensions, Platform, PixelRatio } from 'react-native';
const { width } = Dimensions.get('window');

const basePx = Platform.OS === 'ios' ? 683 : 683;

export const Px2Dp = function px2dp(px: number): number {
    const layoutSize = (px / basePx) * width;

    return PixelRatio.roundToNearestPixel(layoutSize);
};

export const fen_to_yuan = function (money) {
    return ((parseInt(money) || 0) / 100).toFixed(2);
};
export const fen_money = function (money) {
    return (parseFloat(money) || 0).toFixed(2);
};

export const getNowFormatDate = function () {
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let strDate = date.getDate();
    let hour = date.getHours();
    let minute = date.getMinutes();
    let second = date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    let currentDate = year + '-' + month + '-' + strDate + ' ' + hour + ':' + minute + ':' + second;

    return currentDate;
};


Math.signFigures = function(num, rank = 20) {
    if(!num) return(0);
    const sign = num / Math.abs(num);
    const number = num * sign;
    const temp = rank - 1 - Math.floor(Math.log10(number));
    let ans;
    if (temp > 0) {
        ans = parseFloat(number.toFixed(temp));
    }
    else if (temp < 0) {
        ans = Math.round(number / Math.pow(10, temp)) * temp;
    }
    else {
        ans = Math.round(number);
    }
    return (ans * sign);
};

export const is = {
    has(val) {
        return !!val;
    },
    not(val) {
        return !val;
    },
    empty(val) {
        return val === '';
    },
    age(val){
        return val && /^[0-9]{1,2}$/.test(val);
    },
    qq(val) {
        return val &&　/^[1-9]\d{4,12}$/.test(val);
    },
    number(val) {
      return val && /^\+?[1-9][0-9]*$/.test(val);
    },
    name(val) {
        return val &&　/^[A-Za-z0-9\u4E00-\u9FA5_]{2,10}$/.test(val);
    },
    mobile(val) {
        return val && /^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/.test(val);
    },
    email(val) {
        return val && /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(val);
    },
    password(val) {
        return val && val.length && val.length > 5 && !/^\d+$/.test(val);
    },
    time(val){
        return val && val.split(' ')[0]
    }
};